function Ball(){
	this.hue = Math.random()*360;
	this.dia = 40+Math.random()*110;
	this.r = this.dia/2;
	this.left = ($(window).width()-this.dia)*Math.random();
	this.top1 = ($(window).height()-this.dia)*Math.random();
	
	this.$ele = $("<div></div>").css({
		width:this.dia,
		height:this.dia,
		"border-radius":"50%",
		position:"absolute",
		border:"solid 1px",
		"border-color":"hsl("+this.hue+",100%,50%)",
		"box-shadow":"5px 5px "+this.dia+"px -"+this.dia/10+"px hsl("+this.hue+",100%,50%)"
	}).appendTo("body").offset({left:this.left,top:this.top1});
	
	this.xSpeed = Math.random()*5-3;
	this.xSpeed = this.xSpeed>-0.5?this.xSpeed+1:this.xSpeed;
	
	this.ySpeed = Math.random()*5-3;
	this.ySpeed = this.ySpeed>-0.5?this.ySpeed+1:this.ySpeed;
	
	//定义一个数组，用于存放和自己碰撞的其他球
	this.touchBalls = [];
};

Ball.prototype.move = function(){
	
	var x = this.$ele.offset().left;
	var y = this.$ele.offset().top;
	
	if(x+this.xSpeed<=0){
		this.xSpeed = Math.abs(this.xSpeed);
	}
	
	if(x+this.xSpeed>=$(window).width()-this.dia){
		this.xSpeed = -Math.abs(this.xSpeed);
	}
	
	if(y+this.ySpeed<=0){
		this.ySpeed = Math.abs(this.ySpeed);
	}
	
	if(y+this.ySpeed>=$(window).height()-this.dia){
		this.ySpeed = -Math.abs(this.ySpeed);
	}
	
	x+=this.xSpeed;
	y+=this.ySpeed;
	
	this.$ele.offset({
		left:x,
		top:y
	});
	
	//通过requestAnimationFrame延时执行的函数，其调用者是window,
	//所以再次调用move函数时，就不能通过this,$ele找到小球元素的jquery
	//容器了。
	//requestAnimationFrame(this.call);
	
	//函数的.bind方法，是指定调用者，生成一个新的函数，新函数和原函数
	//的代码相同，其中的this指向bind函数传递的参数对象。
	//requestAnimationFrame(Ball.prototype.move.bind(this));
}

Ball.prototype.isTouchWith = function(otherBall){
	this.center = {
		x:this.$ele.offset().left+this.r,
		y:this.$ele.offset().top+this.r
	};
	
	otherBall.center = {
		x:otherBall.$ele.offset().left+otherBall.r,
		y:otherBall.$ele.offset().top+otherBall.r
	};
	
	var xd = this.center.x-otherBall.center.x;
	var yd = this.center.y-otherBall.center.y;
	
	var distance = Math.sqrt(xd*xd+yd*yd);
	
	//如果圆心距离小于等于两球半径之和，说明碰撞了
	if(distance<=this.r+otherBall.r){
		//当两球碰撞时，如果之前没有处理过碰撞，就处理碰撞并将otherBall
		//记录在this的数组中，表示本次碰撞已经处理。
		if(this.touchBalls.indexOf(otherBall) == -1){
			this.touchBalls.push(otherBall);
			//如果两球x,y轴方向都一样碰撞了，说明是追尾碰撞，交换两个球的
			//速度。
			if(this.xSpeed*otherBall.xSpeed>0&&this.ySpeed*otherBall.ySpeed>0){
				var temp = this.xSpeed;
				this.xSpeed = otherBall.xSpeed;
				otherBall.xSpeed = temp;
				
				var temp = this.ySpeed;
				this.ySpeed = otherBall.ySpeed;
				otherBall.ySpeed = temp;
			}else{
				//如果不是x,y轴方向都一样，那么将不同方向的速度取反即可
				if(this.xSpeed*otherBall.xSpeed<0){
					//如果x轴方向不同，将x轴速度取反
					this.xSpeed = -this.xSpeed;
					otherBall.xSpeed = -otherBall.xSpeed;
				}
				if(this.ySpeed*otherBall.ySpeed<0){
					//如果y轴方向不同，将y轴速度取反
					this.ySpeed = -this.ySpeed;
					otherBall.ySpeed = -otherBall.ySpeed;
				}
			}
		}
	}
	
	//当两球离开一定距离时，把otherBall从this的数组中删除，准备处理
	//下次碰撞。
	if(distance>this.r+otherBall.r+6){
		var index = this.touchBalls.indexOf(otherBall);
		if(index>=0){
			this.touchBalls.splice(index,1);
		}
	}
	
}
